package com.hq.ims.data.service;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.hq.ims.data.config.verification.VdAdd;
import com.hq.ims.data.config.verification.VdEdit;
import com.hq.ims.data.dao.BusiIndustryCompanyMapper;
import com.hq.ims.data.entity.BusInfoNotification;
import com.hq.ims.data.entity.BusiIndustryCompany;
import com.hq.ims.data.entityenum.EnumAuditStatus;
import com.hq.ims.data.entityenum.EnumFeedbackPhase;
import com.hq.ims.data.entityenum.EnumPublishStatus;
import com.hq.ims.data.vo.BusiIndustryAssignMapstruct;
import com.hq.ims.data.vo.BusiIndustryCompanyMapstruct;
import com.hq.ims.data.vo.BusiInfoMapstruct;
import com.hq.mybatis.base.BaseService;
import com.hq.mybatis.base.page.RequestPage;
import com.hq.utils.util.ObjectUtils;
import com.hq.web.utils.SpringBootValidateUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 蒋柏平
 * @detail 产业标记公司表 服务实现类
 * @since 20241025
 */
@Service
public class BaseBusiIndustryCompanyService extends BaseService<BusiIndustryCompanyMapper, BusiIndustryCompany> {
    @Resource
    private BaseBusInfoNotificationService baseBusInfoNotificationService;

    /**
     * 分页
     */
    public List<BusiIndustryCompanyMapstruct.BusiIndustryCompanyVo> getByPage(
            RequestPage page,
            BusiIndustryCompanyMapstruct.PageSearchVo searchVo
    ) {
        //验证
        LambdaQueryChainWrapper<BusiIndustryCompany> lq = this.lambdaQuery();
        //排序
        //查询条件

        //执行分页
        lq.page(page);

        List<BusiIndustryCompany> dataList = page.getRecords();
        List<BusiIndustryCompanyMapstruct.BusiIndustryCompanyVo> voList = BusiIndustryCompanyMapstruct.INSTANCE.toBusiIndustryCompanyVoList(dataList);
        page.setRecords(voList);
        return voList;
    }

    /**
     * 添加
     */
    @Transactional(rollbackFor = Exception.class)
    public BusiIndustryCompany addData(BusiIndustryCompanyMapstruct.SaveVo vo) {
        //验证
        SpringBootValidateUtils.validate(vo, VdAdd.class);

        //账值新的对象
        BusiIndustryCompany entity = new BusiIndustryCompany();
        entity.setIndustryId(vo.getIndustryId());
        entity.setIndustryName(vo.getIndustryName());
        entity.setCompanyId(vo.getCompanyId());
        entity.setCompanyName(vo.getCompanyName());
        entity.setBusiInfoId(vo.getBusiInfoId());
        entity.setNotificationWay(vo.getNotificationWay());
        entity.insert();
        return entity;
    }

    /**
     * 编辑
     */
    @Transactional(rollbackFor = Exception.class)
    public BusiIndustryCompany editData(BusiIndustryCompanyMapstruct.SaveVo vo) {
        //验证
        SpringBootValidateUtils.validate(vo, VdEdit.class);

        //账值新的对象
        BusiIndustryCompany entity = this.getById(vo.getBusiIndustryCompanyId());
        ObjectUtils.isNullThrow(entity, "数据不存在");

        entity.setIndustryId(vo.getIndustryId());
        entity.setIndustryName(vo.getIndustryName());
        entity.setCompanyId(vo.getCompanyId());
        entity.setCompanyName(vo.getCompanyName());
        entity.setBusiInfoId(vo.getBusiInfoId());
        entity.setNotificationWay(vo.getNotificationWay());
        entity.updateById();
        return entity;
    }


    /**
     * 删除
     */
    @Transactional(rollbackFor = Exception.class)
    public void delData(String busiIndustryCompanyId) {
        BusiIndustryCompany data = this.getById(busiIndustryCompanyId);
        if (data == null) {
            return;
        }
        data.deleteById();
    }

    /**
     * 获取原始数据详情
     */
    public BusiIndustryCompanyMapstruct.BusiIndustryCompanyVo getDetailVo(String busiIndustryCompanyId) {
        BusiIndustryCompany entity = this.getById(busiIndustryCompanyId);
        BusiIndustryCompanyMapstruct.BusiIndustryCompanyVo vo = BusiIndustryCompanyMapstruct.INSTANCE.toBusiIndustryCompanyVo(entity);
        return vo;
    }

    /**
     * @description: 拼接推送公司及接收情况
     * @author: yinzhangxiao
     * @date: 2024/11/17 16:26
     */
    public void montageCompany(BusiIndustryAssignMapstruct.BusiIndustryAssignVo busiIndustryAssignVo) {
        //产业标记推送公司及接收情况
        List<BusiIndustryCompany> companyList = this.lambdaQuery()
                .eq(BusiIndustryCompany::getBusiInfoId, busiIndustryAssignVo.getBusiInfoId())
                .eq(BusiIndustryCompany::getIndustryId, busiIndustryAssignVo.getIndustryId())
                .list();
        List<BusiIndustryCompanyMapstruct.BusiIndustryCompanyVo> busiIndustryCompanyVoList = BusiIndustryCompanyMapstruct.INSTANCE.toBusiIndustryCompanyVoList(companyList);
        //信息类型为审核通过或已发布,查询产业标记公司接收信息
        if (CollectionUtils.isNotEmpty(busiIndustryCompanyVoList)) {
            busiIndustryCompanyVoList.forEach(busiIndustryCompanyVo -> {
                BusInfoNotification notification = baseBusInfoNotificationService.lambdaQuery()
                        .eq(BusInfoNotification::getIndustryMarkId, busiIndustryAssignVo.getIndustryId())
                        .eq(BusInfoNotification::getBusiInfoId, busiIndustryAssignVo.getBusiInfoId())
                        .eq(BusInfoNotification::getCompanyId, busiIndustryCompanyVo.getCompanyId())
                        .one();
                if (notification != null) {
                    busiIndustryCompanyVo.setReceiveStatus(notification.getReceiveStatus());
                    busiIndustryCompanyVo.setPushDate(notification.getPushDate());
                    busiIndustryCompanyVo.setReasonForRejection(notification.getReasonForRejection());
                    busiIndustryCompanyVo.setHandleDate(notification.getHandleDate());
                }
            });
        }
        busiIndustryAssignVo.setBusiIndustryCompanyList(busiIndustryCompanyVoList);
    }
}